﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.So;
using nERP.Model;
using nERP.Model.Bas;

namespace nERP.ViewModel.So.SaleDetailVMs
{
    public partial class SaleDetailListVM : BasePagedListVM<SaleDetail_View, SaleDetailSearcher>
    {

        protected override IEnumerable<IGridColumn<SaleDetail_View>> InitGridHeader()
        {
            return new List<GridColumn<SaleDetail_View>>{

                this.MakeGridHeader(x => x.SaleDetail_Seq).SetTitle(@Localizer["Page.序号"].Value),
                this.MakeGridHeader(x => x.SaleDetail_Sale).SetTitle(@Localizer["Page.销售单号"].Value),
                this.MakeGridHeader(x => x.SaleDetail_SaleDetailCode).SetTitle(@Localizer["Page.明细单号"].Value),
                this.MakeGridHeader(x => x.SaleDetail_Goods).SetTitle(@Localizer["Page.商品"].Value),
                this.MakeGridHeader(x => x.SaleDetail_GoodsName).SetTitle(@Localizer["Page.商品名称"].Value),
                this.MakeGridHeader(x => x.SaleDetail_GoodsSpec).SetTitle(@Localizer["Page.商品规格"].Value),
                this.MakeGridHeader(x => x.SaleDetail_GoodsUnit).SetTitle(@Localizer["Page.计量单位"].Value),
                this.MakeGridHeader(x => x.SaleDetail_SaleNumber).SetTitle(@Localizer["Page.数量"].Value),
                this.MakeGridHeader(x => x.SaleDetail_UnitPrice).SetTitle(@Localizer["Page.单价"].Value),
                this.MakeGridHeader(x => x.SaleDetail_TotalPrice).SetTitle(@Localizer["Page.金额"].Value),
                this.MakeGridHeader(x => x.SaleDetail_TaxRate).SetTitle(@Localizer["Page.税率"].Value),
                this.MakeGridHeader(x => x.SaleDetail_DiscountPrice).SetTitle(@Localizer["Page.折扣金额"].Value),
                this.MakeGridHeader(x => x.SaleDetail_TaxUnitPrice).SetTitle(@Localizer["Page.含税单价"].Value),
                this.MakeGridHeader(x => x.SaleDetail_TaxPrice).SetTitle(@Localizer["Page.税额"].Value),
                this.MakeGridHeader(x => x.SaleDetail_TotalTaxPrice).SetTitle(@Localizer["Page.含税总价"].Value),
                this.MakeGridHeader(x => x.SaleDetail_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.SaleDetail_Status).SetTitle(@Localizer["Page.状态"].Value),
                this.MakeGridHeader(x => x.SaleDetail_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.SaleDetail_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.SaleDetail_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.SaleDetail_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),

                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<SaleDetail_View> GetSearchQuery()
        {
            var query = DC.Set<SaleDetail>()

                .CheckEqual(Searcher.Seq, x => x.Seq)
                .CheckEqual(Searcher.SaleId, x => x.SaleId)
                .CheckContain(Searcher.SaleDetailCode, x => x.SaleDetailCode)
                .CheckEqual(Searcher.GoodsId, x => x.GoodsId)
                .CheckContain(Searcher.GoodsName, x => x.GoodsName)
                .CheckContain(Searcher.GoodsSpec, x => x.GoodsSpec)
                .CheckContain(Searcher.GoodsUnit, x => x.GoodsUnit)
                .CheckEqual(Searcher.SaleNumber, x => x.SaleNumber)
                .CheckEqual(Searcher.UnitPrice, x => x.UnitPrice)
                .CheckEqual(Searcher.TotalPrice, x => x.TotalPrice)
                .CheckEqual(Searcher.TaxRate, x => x.TaxRate)
                .CheckEqual(Searcher.DiscountPrice, x => x.DiscountPrice)
                .CheckEqual(Searcher.TaxUnitPrice, x => x.TaxUnitPrice)
                .CheckEqual(Searcher.TaxPrice, x => x.TaxPrice)
                .CheckEqual(Searcher.TotalTaxPrice, x => x.TotalTaxPrice)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .CheckEqual(Searcher.Status, x => x.Status)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x => x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x => x.UpdateBy)
                .Select(x => new SaleDetail_View
                {
                    ID = x.ID,

                    SaleDetail_Seq = x.Seq,
                    SaleDetail_Sale = DC.Set<Sale>().Where(z0 => z0.SaleCode == x.SaleId).Select(y => y.SaleCode).FirstOrDefault(),
                    SaleDetail_SaleDetailCode = x.SaleDetailCode,
                    SaleDetail_Goods = x.GoodsId,//DC.Set<Goods>().Where(z0 => z0.GoodsCode == x.GoodsId).Select(y => y.GoodsName).FirstOrDefault(),
                    SaleDetail_GoodsName = x.GoodsName,
                    SaleDetail_GoodsSpec = x.GoodsSpec,
                    SaleDetail_GoodsUnit = x.GoodsUnit,
                    SaleDetail_SaleNumber = x.SaleNumber,
                    SaleDetail_UnitPrice = x.UnitPrice,
                    SaleDetail_TotalPrice = x.TotalPrice,
                    SaleDetail_TaxRate = x.TaxRate,
                    SaleDetail_DiscountPrice = x.DiscountPrice,
                    SaleDetail_TaxUnitPrice = x.TaxUnitPrice,
                    SaleDetail_TaxPrice = x.TaxPrice,
                    SaleDetail_TotalTaxPrice = x.TotalTaxPrice,
                    SaleDetail_Remark = x.Remark,
                    SaleDetail_Status = x.Status,
                    SaleDetail_CreateTime = x.CreateTime,
                    SaleDetail_UpdateTime = x.UpdateTime,
                    SaleDetail_CreateBy = x.CreateBy,
                    SaleDetail_UpdateBy = x.UpdateBy,
                })
                .OrderBy(x => x.ID);
            return query;
        }


    }
    public class SaleDetail_View : SaleDetail
    {

        public int? SaleDetail_Seq { get; set; }
        public string SaleDetail_Sale { get; set; }
        public string SaleDetail_SaleDetailCode { get; set; }
        public string SaleDetail_Goods { get; set; }
        public string SaleDetail_GoodsName { get; set; }
        public string SaleDetail_GoodsSpec { get; set; }
        public string SaleDetail_GoodsUnit { get; set; }
        public decimal? SaleDetail_SaleNumber { get; set; }
        public decimal? SaleDetail_UnitPrice { get; set; }
        public decimal? SaleDetail_TotalPrice { get; set; }
        public decimal? SaleDetail_TaxRate { get; set; }
        public decimal? SaleDetail_DiscountPrice { get; set; }
        public decimal? SaleDetail_TaxUnitPrice { get; set; }
        public decimal? SaleDetail_TaxPrice { get; set; }
        public decimal? SaleDetail_TotalTaxPrice { get; set; }
        public string SaleDetail_Remark { get; set; }
        public dStatusTypeEnum? SaleDetail_Status { get; set; }
        public DateTime? SaleDetail_CreateTime { get; set; }
        public DateTime? SaleDetail_UpdateTime { get; set; }
        public string SaleDetail_CreateBy { get; set; }
        public string SaleDetail_UpdateBy { get; set; }

    }

}